package org.yds.quality.function;


import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorLong;
import com.googlecode.aviator.runtime.type.AviatorObject;

import java.util.Map;

public class SizeSumFunction extends AbstractFunction {

    @Override
    public AviatorObject call(Map<String, Object> env, AviatorObject defectSize, AviatorObject defectLimit) {
        // 获取参数值
        int size = ((Number) defectSize.getValue(env)).intValue();
        int limit = ((Number) defectLimit.getValue(env)).intValue();
        long total = ((Number) env.getOrDefault("total", 0)).longValue();
        // 判断是否触发缺陷标准
        boolean isDefective = size > limit;

        // 如果触发缺陷标准，累加总数
        if (isDefective) {
            // 获取当前总数（如果不存在则初始化为 0）
            total++;
        }

        // 返回累加后的总数
        return AviatorLong.valueOf(total);
    }

    @Override
    public String getName() {
        return "SizeSum"; // 函数名称
    }
}
